﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using ECare.Client;
using ECare.BUS.Data;
using ECare.BUS;
using ECare.Client.templates;


namespace ECare.Client.frms
{
    public partial class frmCreateStorage : frmInfo
    {
        public frmCreateStorage()
        {
            InitializeComponent();

            this.Load += frmCreateStorage_Load;

            this.cbStorage.SelectedValueChanged += cbStorage_SelectedValueChanged;

            this.grd.CellDoubleClick += grd_CellDoubleClick;
            this.grd.CellClick += grd_CellClick;
        }

        void grd_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            IsNew = false;
            this.groupBox1.Visible = true;

            var entitye = this.grd.Rows[e.RowIndex].DataBoundItem as StorageTempInfo;
            this.cbStorage.SelectedValue = entitye.stacker_id;
            this.txtNumber.Value = entitye.stacker_number;
            this.txtBarcode.Text = entitye.stacker_barcode;

            this.cbStorage.Enabled = false;
        }

        void grd_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            ////throw new NotImplementedException();
            if (MessageBox.Show("您要删除这个托盘的数据吗?", "信息提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.No)
                return;

            var entitye = this.grd.Rows[e.RowIndex].DataBoundItem as StorageTempInfo;
            this.source.Remove(entitye);
        }


        private void SetMax()
        {
            int max = (this.entity as SupplyInfo).supply_number;
            int left = this.source.Sum(a => a.stacker_number);
            this.txtNumber.Maximum = max - left;

            this.lblMax.Text = string.Format("({0})", max - left);
        }

        void cbStorage_SelectedValueChanged(object sender, EventArgs e)
        {
            //throw new NotImplementedException();
            var currentsupply = this.entity as SupplyInfo;
            //    if(string.IsNullOrEmpty(this.txtBarcode.Text))
            this.txtBarcode.Text = DateTime.Now.ToString("yyyyMMddhhmmss") + "-" + currentsupply.resource_id + "-" + this.cbStorage.SelectedValue.ToString();

        }

        void frmCreateStorage_Load(object sender, EventArgs e)
        {
            //throw new NotImplementedException();
            this.LoadStorage();
            //         this.txtNumber.Maximum = (this.entity as SupplyInfo).supply_number;

            this.grd.DataSource = this.source;
            this.SetMax();
        }
        public BindingList<StorageTempInfo> source = new BindingList<StorageTempInfo>();
        private void LoadStorage()
        {
            var dt = StackerInfoHelper.GetStackers();
            this.cbStorage.DataSource = dt;
            this.cbStorage.DisplayMember = "StackerInfo_Name";
            this.cbStorage.ValueMember = "StackerInfo_Id";
        }

        public bool IsNew = true;
        private void eButton1_Click(object sender, EventArgs e)
        {
            this.groupBox1.Visible = true;
            this.txtNumber.Value = 0;
            this.txtBarcode.Text = string.Empty;
            this.cbStorage.Enabled = true;

        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            #region validate errors
            if (this.cbStorage.SelectedValue == null)
            {
                this.errorProvider1.SetError(this.cbStorage, "empty?");
                return;
            }
            else
            {
                this.errorProvider1.SetError(this.cbStorage, string.Empty);
            }

            if (this.txtNumber.Value == 0)
            {
                this.errorProvider1.SetError(this.txtNumber, "empty?");
                return;
            }
            else
            {
                this.errorProvider1.SetError(this.txtNumber, string.Empty);
            }
            if (!this.ValidateTextControl(this.txtBarcode))
                return;
            #endregion
            if (IsNew)
            {
                int stackerId = int.Parse(this.cbStorage.SelectedValue.ToString());
                if (this.source.FirstOrDefault(a => a.stacker_id == stackerId) != null)
                {
                    this.errorProvider1.SetError(this.cbStorage, "您当前选中的托盘已经放置了物料.");
                    return;
                }
                else
                {
                    this.errorProvider1.SetError(this.cbStorage, string.Empty);
                }
                var newdetail = new StorageTempInfo();
                newdetail.stacker_barcode = this.txtBarcode.Text;
                newdetail.stacker_id = int.Parse(this.cbStorage.SelectedValue.ToString());
                newdetail.stacker_number = (int)this.txtNumber.Value;
                newdetail.stacker_name = this.cbStorage.Text;
                this.source.Add(newdetail);
            }
            else
            {
                var newdetail = this.grd.SelectedRows[0].DataBoundItem as StorageTempInfo;
                newdetail.stacker_barcode = this.txtBarcode.Text;
                newdetail.stacker_id = int.Parse(this.cbStorage.SelectedValue.ToString());
                newdetail.stacker_number = (int)this.txtNumber.Value;
                newdetail.stacker_name = this.cbStorage.Text;
            }
            this.groupBox1.Visible = false;

            this.SetMax();
        }


        private void eButton1_Click_1(object sender, EventArgs e)
        {
            this.groupBox1.Visible = false;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            //if (DialogResult.Cancel==MessageBox.Show("请您确认好库存数据，这将生成真实库存.", "信息提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning))
            //{
            //    return;
            //}

            if (this.grd.Rows.Count == 0)
                return;

            int current = this.source.Sum(a => a.stacker_number);

            if (current != (entity as SupplyInfo).supply_number)
            {
                MessageBox.Show("请检查您的配置信息，数量对不应.", "信息提示", MessageBoxButtons.OK);
                return;
            }

            var createsult = SupplyInfoHelper.CreateStage((this.entity as SupplyInfo), this.source.ToList());

            (this.entity as SupplyInfo).HasStorage = createsult;

            this.Close();
        }

    }
}
